//
//  MemberListViewController.m
//  YunDongAdmin
//
//  Created by lqq on 2018/9/6.
//  Copyright © 2018年 cloudfighting.com. All rights reserved.
//

#import "MemberListViewController.h"
#import "MemberListCell.h"
#import "ContactDataHelper.h"
#import "MemberListFooterView.h"
#import "I_Member.h"
#import "MemberDetailViewController.h"
@interface MemberListViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) NSMutableArray *serverDataArr;//数据源
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSMutableArray *sectionTitles;
//@property (nonatomic,strong) NSMutableArray *dataArr;
@property (nonatomic, strong) UITableView *tableView;
@end

@implementation MemberListViewController


#pragma mark Class Method
+ (instancetype)controllerWithMemberList:(NSMutableArray *)memberList
{
    MemberListViewController *memberListVC = [[MemberListViewController alloc] init];
    memberListVC.serverDataArr = memberList;
    return memberListVC;
}


#pragma mark Life Circle
- (void)viewDidLoad {
    [super viewDidLoad];
    [self configUI];
    self.title = @"会员";
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self requestMemberList];
}

#pragma mark Config/UI
- (void)configUI{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    tableView.delegate = self;
    tableView.dataSource = self;
    //修改右边索引的背景色
    tableView.sectionIndexBackgroundColor = [UIColor clearColor];
    //修改右边索引字体的颜色
    tableView.sectionIndexColor = HEXColor(@"#4A4A4A");
    //修改右边索引点击时候的背景色
    //    tableView.sectionIndexTrackingBackgroundColor = HEXColor(@"#3AA7FF");
    [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([MemberListCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([MemberListCell class])];
    tableView.backgroundColor = HEXColor(@"F4F6F9");
    [self.view addSubview:tableView];
    [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.tableFooterView = [UIView new];
    self.tableView = tableView;
}
#pragma mark NET
- (void)requestMemberList
{
    if (_dataSource.count == 0) {
        [self.view showLoading];
    }
    WeakSelf(self);
    [I_Member requestMemberListOnSuccess:^(NSMutableArray *list) {
        if (list.count == 0) {
            [weakself.view showEmptyViewWithPic:IMG_NAME(@"pic_novip") desc:@"暂无会员\n添加教练后，即可在【云动助教】添加会员"];
        } else {
            weakself.serverDataArr = list;
            dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                weakself.dataSource = [ContactDataHelper getMemberListDataBy:weakself.serverDataArr];
                weakself.sectionTitles = [ContactDataHelper getMemberListSectionBy:weakself.dataSource];
                dispatch_sync(dispatch_get_main_queue(), ^{
                    [weakself.view hideStatusView];
                    [weakself.tableView reloadData];
                    weakself.tableView.tableFooterView = [MemberListFooterView viewForMemberCount:weakself.serverDataArr.count];
                });
            });
        }
        
    } failure:^(NSError *error) {
        if (weakself.dataSource.count == 0) {
            [weakself.view showError:error refreshBlock:^{
                [weakself requestMemberList];
            }];
        }
    }];
}

#pragma mark Action



#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return _dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [_dataSource[section] count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    MemberListCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([MemberListCell class])];
    M_Member *member = self.dataSource[indexPath.section][indexPath.row];
    cell.member = member;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 25;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *label = [[UILabel alloc] init];
    [label setFont:MediumFont(14)];
    [label setTextColor:HEXColor(@"4A4A4A")];
    [label setBackgroundColor:HEXColor(@"F4F6F9")];
    [label setText:[NSString stringWithFormat:@"  %@",self.sectionTitles[section]]];
    return label;
    
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView{
    return self.sectionTitles;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    M_Member *member = self.dataSource[indexPath.section][indexPath.row];
    MemberDetailViewController *detailVC = [MemberDetailViewController controllerWithMemberId:member.memberId];
    [self.navigationController pushViewController:detailVC animated:YES];

}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

@end
